package com.funny.stock.trade.service;

import com.funny.stock.base.service.BaseCrudService;
import com.funny.stock.data.vo.StockConstants;
import com.funny.stock.trade.dao.pojo.GridTradePlanVarDO;
import com.funny.stock.trade.dao.repo.GridTradePlanVarRepo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

/**
 * @Description  
 * @Author  pengl
 * @Date 2020-11-18
 */

@Service
public class GridTradePlanVarService extends BaseCrudService<GridTradePlanVarDO,Long> {

	@Autowired
	GridTradePlanVarRepo repository;

	protected JpaRepository<GridTradePlanVarDO, Long> setupJpaRepository() {
		return repository;
	}


//	public GridTradePlanVarDO findOne(Long id) {
//		return repository.findByPlanId(id);
//	}
	@Cacheable(value = StockConstants.KEY_GRID_TRADE_VAR_POOL, key = "#planId", unless="#result == null")
	public GridTradePlanVarDO findByPlanId(Long planId){
		return repository.findByPlanId(planId);
	}

	@Caching(
			put = {@CachePut(value = StockConstants.KEY_GRID_TRADE_VAR_POOL,
					key = "#tradePlanVarDO.planId", condition = "#result != null")}
			)
	public GridTradePlanVarDO savePlan(GridTradePlanVarDO tradePlanVarDO){
		if(tradePlanVarDO.getId() == null){
			GridTradePlanVarDO queryDO = findByPlanId(tradePlanVarDO.getPlanId());
			if(queryDO != null) tradePlanVarDO.setId(queryDO.getId());
		}

		return repository.save(tradePlanVarDO);
	}
}
